class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0, ans = nums.length + 1, count = 0, left = 0;

        for(int i = 0; i < nums.length; i++) {
            sum +=nums[i];
            count++;

            while(sum >= target) {
                sum -= nums[left++];
                ans = Math.min(ans, count--);
            }
        }
        return ans > nums.length ? 0 : ans;
    }
}
